package ext.acae2.baseline.autonumber;

import org.apache.log4j.Logger;
import wt.inf.container.WTContainerRef;
import wt.log4j.LogR;
import wt.rule.ruleResource;
import wt.rule.algorithm.InvalidAlgorithmArgumentException;
import wt.rule.algorithm.RuleAlgorithm;
import wt.util.WTException;
import wt.util.WTMessage;

/**
 * @desription TODO
 * @author cnie
 * @date 2013-3-4 上午11:47:21
 */
public class AcaeProductCodeGenerator implements RuleAlgorithm {

	private static final String RULE_RESOURCE = ruleResource.class.getName();
	private static final Logger LOGGER = LogR.getLogger(AcaeProductCodeGenerator.class
			.getName());
	static final String SPILT_STR = "-";

	/*
	 * (non-Javadoc)
	 * 
	 * @see wt.rule.algorithm.RuleAlgorithm#calculate(java.lang.Object[],
	 * wt.inf.container.WTContainerRef)
	 */
	@Override
	public Object calculate(Object[] params, WTContainerRef containerRef)
			throws WTException {
		for (int i = 0; i < params.length; i++) {
			if (params[i] == null) {
				String nullValue = WTMessage.getLocalizedMessage(RULE_RESOURCE,
						ruleResource.NULL_ARG);
				throw new InvalidAlgorithmArgumentException(RULE_RESOURCE,
						nullValue, new Object[] { Integer.valueOf(i) });
			}
		}
		String productCode = (String) params[0];
		LOGGER.debug("=========productCode===========>" + productCode);
		int spiltIndex = productCode.indexOf(SPILT_STR);
		String result = String.valueOf(productCode.charAt(0))
				+ String.valueOf(productCode.charAt(spiltIndex + 1))
				+ String.valueOf(productCode.charAt(productCode.length() - 1));

		
		return result;
	}

}
